﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
import flash.external.ExternalInterface;
import mx.utils.Delegate;
class Bar {
	private static var formatter:TextFormat;
	public var id:String;
	public var name:String;
	public var color:Number;
	public var value:Number;
	public function Bar(id:String, name:String, color:Number, value:Number) {
		this.id = id;
		this.name = name;
		this.color = color;
		this.value = value;
	}
	public function draw(mc:MovieClip, config:Config, i:Number, n:Number):Void {
		//transform
		var width = Stage.width-config.leftSpace-36;
		var height = Stage.height-36;
		var xl:Number = config.leftSpace+20+width*(i+1)/(n+1)-width/(n+1)/3;
		var xr:Number = config.leftSpace+20+width*(i+1)/(n+1)+width/(n+1)/3;
		var yb:Number = Stage.height-20;
		var yt:Number = Stage.height-20-value*height/config.yBound;
		//draw
		mc.lineStyle(1, color, 100, true, "normal", "square", "miter", 0);
		mc.beginFill(color, 100);
		mc.moveTo(xl, yb-1);
		mc.lineTo(xr, yb-1);
		mc.lineTo(xr, yt);
		mc.lineTo(xl, yt);
		mc.lineTo(xl, yb-1);
		mc.endFill();
		//formatter
		if (formatter == undefined) {
			formatter = new TextFormat();
			formatter.size = 12;
			formatter.color = config.textColor;
		}
		//name     
		var fontSize:Object = formatter.getTextExtent(String(value));
		var xPos:Number = config.leftSpace+20+width*(i+1)/(n+1);
		var tField = mc.createTextField("tField", mc.getNextHighestDepth(), xPos-fontSize.textFieldWidth/2, yt-20+2, fontSize.textFieldWidth, fontSize.textFieldHeight);
		tField.selectable = false;
		tField.text = String(value);
		tField.setTextFormat(formatter);
		//click event
		mc.onRelease = Delegate.create(this, onRelease);
	}
	public function onRelease():Void {
		ExternalInterface.call(Constants.BarClickCallBack, Constants.FlashObjID, id, name, value);
	}
}
